package com.example.myapplication.module2

import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView.VERTICAL
import com.example.myapplication.R
import com.example.myapplication.databinding.FragmentPswBinding
import com.example.myapplication.entity.ItemBean
import com.example.myapplication.module1.ItemGridSpanSizeLookup
import com.example.myapplication.module1.adapter.CategoryContentAdapter
import com.example.mylibrary.base.BaseFragment
import com.example.mylibrary.utils.UiTools

/**
 * 进销存
 */
class PswFragment : BaseFragment<FragmentPswBinding>() {
    val mutableList = mutableListOf<Any>()
    val itemBeanList = mutableListOf<ItemBean>()

    private lateinit var pswTitle: Array<String>
    private lateinit var buyManager: Array<String>
    private lateinit var sellManager: Array<String>
    private lateinit var warehouseManager: Array<String>

    private val categoryContentAdapter: CategoryContentAdapter? by lazy {
        activity?.let { CategoryContentAdapter(it) }
    }


    override fun getBinding(): FragmentPswBinding {
        return FragmentPswBinding.inflate(layoutInflater)
    }

    override fun initView(view: View) {

        mutableList.clear()
        pswTitle = UiTools.getStringArray(R.array.pswTitle)
        buyManager = UiTools.getStringArray(R.array.buyManager)
        sellManager = UiTools.getStringArray(R.array.sellManager)
        warehouseManager = UiTools.getStringArray(R.array.warehouseManager)
        val buyManagerImage = UiTools.resources.obtainTypedArray(R.array.buyManagerImage)
        val sellManagerImage = UiTools.resources.obtainTypedArray(R.array.sellManagerImage)
        val warehouseManagerImage =
            UiTools.resources.obtainTypedArray(R.array.warehouseManagerImage)

        val gridLayoutManager = GridLayoutManager(activity, 4, VERTICAL, false)

        for (i in pswTitle.indices) {
            itemBeanList.clear()
            mutableList.add(pswTitle[i])
            when (i) {
                0 -> {
                    for (index in buyManager.indices) {
                        itemBeanList.add(
                            ItemBean(
                                2,
                                i,
                                index,
                                "1$i$index",
                                buyManager[index],
                                buyManagerImage.getResourceId(index, 0)
                            )
                        )
                    }
                }
                1 -> {
                    for (index in sellManager.indices) {
                        itemBeanList.add(
                            ItemBean(
                                2,
                                i,
                                index,
                                "1$i$index",
                                sellManager[index],
                                sellManagerImage.getResourceId(index, 0)
                            )
                        )
                    }
                }
                2 -> {
                    for (index in warehouseManager.indices) {
                        itemBeanList.add(
                            ItemBean(
                                2,
                                i,
                                index,
                                "1$i$index",
                                warehouseManager[index],
                                warehouseManagerImage.getResourceId(index, 0)
                            )
                        )
                    }
                }
            }
            mutableList.addAll(itemBeanList)
        }
        buyManagerImage.recycle()
        sellManagerImage.recycle()
        warehouseManagerImage.recycle()
        gridLayoutManager.spanSizeLookup = ItemGridSpanSizeLookup(mutableList, 4)
        viewBinding.rvList.layoutManager = gridLayoutManager
        viewBinding.rvList.adapter = categoryContentAdapter

        categoryContentAdapter?.mutableList = mutableList
    }


}
